Skip to content

Allow #[ts(flatten)] for unit structs#474

Open
gustavo-shigueo wants to merge 4 commits intomainfrom
flatten_unit
Open

Allow #[ts(flatten)] for unit structs#474
gustavo-shigueo wants to merge 4 commits intomainfrom
flatten_unit

Conversation

@gustavo-shigueo
Copy link
Collaborator

@gustavo-shigueo gustavo-shigueo commented Feb 2, 2026

Goal

Allow the use of #[ts(flatten)] with unit structs struct Foo; and empty structs struct Foo {} but not empty tuple structs struct Foo(). This matches the behavior of #[serde(flatten)]

The use of the type { } when flattening these structs does not cause the {} type to appear in typescript because of our .replace(" } & { ") which we added back when we first changed the implementation of flatten to use type intersections, which means these structs simply disappear from the ts type as though #[ts(skip)] was used instead, which also matches serde behavior.

Allowing flatten on struct Foo; (no curly braces) broke the generation of internally tagged enums because it caused an interaction with the struct version of #[ts(tag = "...")], but this ended up making the enum generation code simpler as it removed an unnecessary match expression and the need to manually add the internal tag, as the struct version of this attribute does it instead

Also added a validation rule to prevent the use of internal tagging (#[ts(tag = "...")] without #[ts(content = "...")]) when the enum contains tuple variants,
Closes #473

Changes

How did you go about solving the problem?

Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

Copy link
Collaborator

@NyxCode NyxCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code-wise this looks good to me. Fixing a bug while simplifying the code is just great.
If you'd like me to review the semantics of this change, then I'll need to put aside some time to look into this more. It's been a while since I worked on that part of the code.

However, if you're confident, you can also merge this as-is for a patch release. Your call!

@gustavo-shigueo
Copy link
Collaborator Author

Hey @NyxCode, sorry I took a while to respond.

If you'd like me to review the semantics of this change, then I'll need to put aside some time to look into this more

While I think this shouldn't break anything, I do think it's worth having you take a look just to make sure :D

However, if you're confident

Nah, I'm never confident in my own code lmao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TS Export Panics When Flattening Zero-Field Struct

2 participants